/*
 * Copyright (C) 2018-2020 Tsing Micro Holding Limited
 */

#ifndef MEDIA_VIDEO_WRAPPER_H
#define MEDIA_VIDEO_WRAPPER_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <drv_dpu.h>
//#include "ov9282_drv.h"
#include "aos/hal/i2c.h"
#include <soc.h>
#include "board.h"

//#define IR_SENSOR_ISP_BYPASS 


#define HAL_Printf printf
#define video_info(...)    do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define video_err(...)     do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define video_debug(...)   do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)

#define  MAX_FACE_NUM                  FACE_FEATURE_MAX_NUM
#define  FACE_FEATURE_POOL_MAX_NUM     FACE_FEATURE_MAX_NUM
#define  FACE_FEATURE_KEY_CURR_MAX     "ff_curr_max"
//#define  FACE_NUM_ADR 		           (uint32_t *)0x7e00000
//#define  FACE_ID_ADR 		           (uint32_t *)0x7e00004
//#define  FACE_FEATURE_ADR 	           (uint8_t *)0x7e00008
//#define  FACE_FEATURE_SIZE 	           512
//#define  FACE_FEATURE_MEM_BASEADDR     FACE_FEATURE_ADR
#define  FACE_FEATURE_SIZE             ONE_FACE_FEATURE_SIZE * 2
#ifndef itoa
#define  itoa(val, str)                sprintf(str, "%d", val)
#endif
#define  ONE_FACE_FEATURE_SIZE         256
#define  FACE_FEATURE_LENGTH           ONE_FACE_FEATURE_SIZE

typedef enum TSM_SNS_TYPE
{
    OMNIVISION_9732_MIPI_720P_30FPS_8BIT,
    OMNIVISION_9282_MIPI_720P_30FPS_8BIT,
    TSM_SNS_TYPE_BUTT,
} TSM_SNS_TYPE_E;

typedef enum TSM_VIDEO_RES_TYPE
{
	VEDEO_RES_1080P,
	VEDEO_RES_720P,
	VEDEO_RES_640x480,
	VEDEO_RES_640x360,
    VEDEO_RES_640x241x5,
    VEDEO_RES_1280x800,
    VEDEO_RES_640x400,
    VEDEO_RES_1280x962,
    VEDEO_RES_360x640,
    VEDEO_RES_800x600,
    VEDEO_RES_1600x1200,
    VEDEO_RES_1080x1280,
    VEDEO_RES_720x1280,
    VEDEO_RES_BUTT,
} TSM_VIDEO_RES_TYPE_E; //resolution

typedef enum TSM_PIXEL_FORMAT
{
	PIXEL_FORMAT_ARGB,
	PIXEL_FORMAT_YUV_422,
	PIXEL_FORMAT_BAYER_RAW10,
	PIXEL_FORMAT_BAYER_RAW8,
	PIXEL_FORMAT_RAW10,
	PIXEL_FORMAT_RAW8,
    PIXEL_FORMAT_RAW12,
    PIXEL_FORMAT_BUTT,
} TSM_PIXEL_FORMAT_E;

typedef enum TSM_DATA_LAYOUT
{
	DATA_LAYOUT_INVALID,
	DATA_LAYOUT_YUV_PLANAR,
	DATA_LAYOUT_SEMIPLANAR,
	DATA_LAYOUT_INTERLEAVED,
    DATA_FORMAT_BUTT,
} TSM_DATA_LAYOUT_E;


typedef enum TSM_SENSOR_EXPO
{
	SENSOR_EXPO_INDOOR,
	SENSOR_EXPO_OUTDOOR,
    SENSOR_EXPO_BUTT,
} TSM_SENSOR_EXPO_E;

typedef struct TSM_SENSOR_INFO
{
    TSM_SNS_TYPE_E          enSnsType;
	TSM_VIDEO_RES_TYPE_E   	enRes;
	TSM_PIXEL_FORMAT_E      enFormat;
    uint32_t                u32SnsId;
    uint32_t                u32I2CId;
    uint32_t                u32PortId;
    uint32_t                u32BusId;
    uint32_t                u32MipiId;
    TSM_SENSOR_EXPO_E       u32Expo;
    uint32_t u32MipiClk;
} TSM_SENSOR_INFO_S;

typedef struct TSM_PIPE_INFO
{
    uint32_t                u32PipeId;
	uint32_t             	cropResXoffst;
	uint32_t             	cropResYoffst;
	TSM_VIDEO_RES_TYPE_E   	enRes;
	TSM_PIXEL_FORMAT_E      enFormat;
    bool                    bIspBypass;
    TSM_PIXEL_FORMAT_E      enPixFmt;
	bool	                isp_flt_enable;
    uint32_t                denoise_level;
    uint32_t                sharpen_level;
    bool	                isp_cproc_enable;
    float                   cproc_contrast; 
    int8_t                  cproc_brightness; 
    float                   cproc_saturation;
    float                   cproc_hue;                  
} TSM_PIPE_INFO_S;

typedef enum TSM_LED_LIGHT_TYPE
{
    LED_LIGHT_TYPE_RGB,
    LED_LIGHT_TYPE_IR,
    LED_LIGHT_TYPE_SPK,
    LED_LIGHT_TYPE_BUTT,
} TSM_LED_LIGHT_TYPE_E;

typedef enum TSM_MIPI_WORK_MODE_TYPE
{
    MIPI_WORK_MODE_RGB,
    MIPI_WORK_MODE_IR,
} TSM_MIPI_WORK_MODE_TYPE_E;

typedef struct TSM_LED_INFO
{
    uint32_t                u32LedId;
    uint32_t                u32PortId;
    uint32_t                u32BusId;
    uint32_t                u32Brightness;
    TSM_LED_LIGHT_TYPE_E    enLightType;
    uint32_t                u32DelayTime;
} TSM_LED_INFO_S;

typedef struct TSM_VI_INFO
{
    uint32_t             u32ViId;
    TSM_SENSOR_INFO_S    stSnsInfo;
    TSM_PIPE_INFO_S      stPipeInfo;
    TSM_LED_INFO_S       stLedInfo;
} TSM_VI_INFO_S;

typedef enum TSM_VO_INTF_TYPE
{
    VO_INTF_CVBS,
    VO_INTF_VGA,
    VO_INTF_BT565,
    VO_INTF_BT1120,
    VO_INTF_HDMI,
    VO_INTF_LCD,
    VO_INTF_LCD_8BIT,
    VO_INTF_LCD_16BIT,
    VO_INTF_LCD_24BIT,
    VO_INTF_MIPI,
    VO_INTF_MIPI_SLAVE,
    VO_INTF_BUTT,
} TSM_VO_INTF_TYPE_E;

typedef enum TSM_VIDEO_FPS
{
	VEDEO_FPS_24,
	VEDEO_FPS_30,
	VEDEO_FPS_50,
	VEDEO_FPS_60,
    VEDEO_FPS_BUTT,
} TSM_VIDEO_FPS_E; //Frame rate

typedef struct TSM_RECT
{
    uint32_t u32X;
    uint32_t u32Y;
    uint32_t u32Width;
    uint32_t u32Height;
} TSM_RECT_S;

typedef struct TSM_RES
{
    uint32_t u32Width;
    uint32_t u32Height;
} TSM_RES_S;

typedef struct TSM_VO_INTF_INFO
{
    TSM_VO_INTF_TYPE_E   enVoIntfType;
    TSM_VIDEO_FPS_E      enIntfFrameRate;
	TSM_RES_S            stRes;
    uint32_t             u32BgColor;
} TSM_VO_INTF_INFO_S;

typedef struct TSM_VO_LAYER_INFO
{
	dpu_colormode_e         enFormat;
    TSM_RECT_S              stDispRect;
    TSM_VIDEO_FPS_E         enDispFrameRate;
    dpu_blendmode_e         enBlend;
}TSM_VO_LAYER_INFO_S;

typedef struct TSM_VO_INFO
{
    uint32_t                u32VoId;
    TSM_VO_INTF_INFO_S      stVoIntfInfo;
    TSM_VO_LAYER_INFO_S     stLayerInfo;
} TSM_VO_INFO_S;

extern i2c_dev_t i2c_devices[CONFIG_IIC_NUM];

extern int setIDStartNo(int idStart);
extern int getIDStartNo(void);
extern int getValidIDNum(void);

/*  
 *  get valid register id list function
 *  @param[out] idList    pointer to the id information.
 *  @param[in] cnt    id count.
 *  @return    0 success other failed
 */
extern int getValidIDList(uint32_t *idList, uint32_t cnt);

/*  
 *  Video input initialization function
 *  @param[in] pstViInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
extern int tsm_vi_start(TSM_VI_INFO_S *pstViInfo);

/*  
 *  Video input deinitialization function
 *  @param[in] pstViInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
extern int tsm_vi_stop(TSM_VI_INFO_S *pstViInfo);

/*  
 *  Video input standby function
 *  @param[in] pstViInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
extern int tsm_vi_pause(TSM_VI_INFO_S *pstViInfo);

/*  
 *  Video input resume from standby function
 *  @param[in] pstViInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
extern int tsm_vi_resume(TSM_VI_INFO_S *pstViInfo);

/*  
 *  Video output initialization function
 *  @param[in] pstVoInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
extern int tsm_vo_start(TSM_VO_INFO_S *pstVoInfo);

/*  
 *  Get dpu display memory address function
 *  @return    dpu display memory address
 */
extern void *tsm_vo_get_disp_memory();

/*  
 *  Face feature initialization function
 *  @param[in] 
 *  @return    0 success other failed
 */
extern int tsm_ff_load();

/*  
 *  Face feature add function
 *  @param[in] pFeature    pointer to the face feature.
 *  @return    ID of face feature
 */
extern int tsm_ff_add(void *pFeature, void *pMaskFeature);


/*  
 *  Face feature get function
 *  @param[in] id          ID of face feature.
 *  @param[in] pFeature    pointer to the face feature.
 */
extern int tsm_ff_get(uint32_t id, void *feature);

/*  
 *  Face feature delete function
 *  @param[in] id          ID of face feature.
 *  @return    0 success other failed
 */
extern int tsm_ff_delete(uint32_t id);

/*  
 *  Face feature clear function
 *  @return    0 success other failed
 */
extern int tsm_ff_clear();

/*  
 *  Face feature id dump function
 */
extern int tsm_ff_dump_id();

/*  
 *  Mipi work mode change function
 *  @param[in] mipi_ID: ID of changed mipi.
 *  @param[in] mipiWorkMode_Flag: mipi work mode.
 *  @return    0 success other failed
 */
extern int tsm_vi_change_mipi_work_mode(uint8_t mipi_ID, uint8_t mipiWorkMode_Flag);

/*
 *  open pephole function
 *  @return    0 success other failed
 */
extern int tsm_open_peephole(void);
/*
 *  close peephole function
 *  @return    0 success other failed
 */
extern int tsm_close_peephole(void);

#endif  /* MEDIA_VIDEO_WRAPPER_H */
